package cn.shlbysj.demo.service.impl;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.common.UuidUtil;
import cn.shlbysj.demo.entity.*;
import cn.shlbysj.demo.mapper.NewsInfoMapper;
import cn.shlbysj.demo.mapper.OnlineExpMapper;
import cn.shlbysj.demo.service.INewsInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("newsInfoService")
@Transactional
public class INewsInfoServiceImpl implements INewsInfoService{
    @Autowired
    private NewsInfoMapper newsInfoDao;

    @Override
    public List<NewsInfo> getList() {
        PageHelper.startPage(1,4);
        NewsInfoExample example=new NewsInfoExample();
        example.setOrderByClause("`create_time` desc");
        NewsInfoExample.Criteria criteria = example.createCriteria();
        Page<NewsInfo> list =(Page<NewsInfo>)newsInfoDao.selectByExample(example);
        return list.getResult();
    }

    @Override
    public NewsInfo getOne(String nId) {
        return newsInfoDao.selectByPrimaryKey(nId);
    }

    @Override
    public PageResult findPage(String title, int page, int rows) {
        //设置分页查询条件
        PageHelper.startPage(page,rows);
        NewsInfoExample example=new NewsInfoExample();
        example.setOrderByClause("`create_time` desc");
        NewsInfoExample.Criteria criteria = example.createCriteria();
        if(title!=null && !"".equals(title.toString())){
            criteria.andTitleEqualTo(title);
        }
        Page<NewsInfo> pageList = (Page<NewsInfo>) newsInfoDao.selectByExample(example);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public Result insert(NewsInfo newsInfo) {
        newsInfo.setId(UuidUtil.getUUID());
        newsInfo.setCreateTime(new Date());
        newsInfo.setAuthor("深海蓝");
        newsInfo.setVisiteCount(UuidUtil.getRand(1000));
        int ret=newsInfoDao.insert(newsInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result update(NewsInfo newsInfo) {
        int ret=newsInfoDao.updateByPrimaryKeySelective(newsInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result delete(String nId) {
        int ret=newsInfoDao.deleteByPrimaryKey(nId);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }
}
